package com.wd.shared.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;


public class GZReader {
    public String fileName;
    private BufferedReader in = null;

    
    public GZReader(String fname) {
        fileName = fname;
    }

    
    public boolean open() {
        try {
            if (fileName.endsWith(".gz")) {
                in = new BufferedReader(new InputStreamReader(
                        new GZIPInputStream(new FileInputStream(fileName))));
            } else {
                in = new BufferedReader(new FileReader(fileName));
            }
        } catch (IOException ex) {
            System.err.println("Error: cannot open file: " + fileName);
            return false;
        }
        return true;
    }

    public boolean close() {
        try {
            in.close();
            return true;
        } catch (IOException ex) {
            System.err.println("Error: Failed to close file: " + fileName);
            return false;
        }

    }

    // gets a line. returns false on end-of-file
    public String getNextLine() throws IOException {
        return in.readLine();
    }

    public static void main(String args[]) throws IOException {
        String path = "E:\\work\\project_J\\Test\\test\\phase2B_T_70_20080325_20080506_20080407.txt.gz";
        GZReader gzReader = new GZReader(path);
        gzReader.open();
        String content = "";
        int number = 1;
        while (content != null) {
            content = gzReader.getNextLine();

            if (content == null) {
                break;
            }

            byte[] buf = content.getBytes();

            System.out.println(number
                    + " -------------------------------------------");

            for (byte b : buf) {
                System.out.print((char) b + " ");
            }

            System.out.print("\n");

            number++;

            if (number > 100) {
                break;
            }
        }
        gzReader.close();
        System.out.println("Total line : " + number);
    }
}
